﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WoodSpawn : MonoBehaviour {

    public List<GameObject> prefabs;
    public List<GameObject> instan;

    

    public bool toSpawn = true;

    public float spawnPointX = 0;

    public float maxMargin;
    public float minMargin;

    public int totalS = 10;

    public GameObject cam;
    private CameraMoved cMove;

    void Awake() {
        cMove = cam.GetComponent<CameraMoved>();
        instan = new List<GameObject>();
    }

	void Update () {


        if (toSpawn)
        {
            for (int i = 0; i < totalS; i++)
            {
                int rndInt = Random.Range(0, 100);
                bool hasMine = false;
                if (rndInt < 15)
                {
                    hasMine = true;
                }
                GameObject gobj = Instantiate(prefabs[0]);
                gobj.transform.position = new Vector2(spawnPointX, 0);
                instan.Add(gobj);

                if (hasMine)
                {
                    GameObject gobjM = Instantiate(prefabs[1]);
                    gobjM.transform.position = new Vector2(spawnPointX + Random.Range(3, 4), 0);
                    instan.Add(gobjM);
                }

                //Debug.Log("spawnPointX: " + spawnPointX);

                spawnPointX += Random.Range(minMargin, maxMargin);
             
            }
            //Debug.Log("instan Count:" + instan.Count);
            toSpawn = false;
        }


        if (cMove.OutLeft(instan[instan.Count-5].transform.position.x))
        {
            toSpawn = true;

            for (int i = 0; i <= instan.Count-5; i++)
            {
                GameObject tmp = instan[i];
                instan.RemoveAt(i);
                Destroy(tmp);
                
                
                i--;           
            }
            //Debug.Log("instan Count:" + instan.Count);
        }
        
	}
}
